/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset.entities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-29
 * 
 */
public class GetOthers {

	public static void main(String[] argus) {
		ArrayList<ISentence> res = find("uri", 72771410);
		for (ISentence s : res) {
			System.out.println(s.getTriples());
		}
	}
	public static ArrayList<String> findEntityIds(String ds_type, int ds_id){
		ArrayList<Integer> docs = null;
		if (ds_type.equals("custom")) {
			docs = CustomDataset.getURLIDsByCustomDatasetId(ds_id);
		} else if (ds_type.equals("uri")) {
			docs = new ArrayList<Integer>();
			docs.add(ds_id);
		}
		return findEntityIds(docs);
	}
	public static ArrayList<ISentence> find(String ds_type, int ds_id) {
		ArrayList<Integer> docs = null;
		if (ds_type.equals("custom")) {
			docs = CustomDataset.getURLIDsByCustomDatasetId(ds_id);
		} else if (ds_type.equals("uri")) {
			docs = new ArrayList<Integer>();
			docs.add(ds_id);
		}
		return find(docs);
	}
	public static ArrayList<String> findEntityIds(ArrayList<Integer> docs){
		ArrayList<String> bnids = new ArrayList<String>();

		String docstring = "";
		for (int doc : docs) {
			docstring += doc + ",";
		}
		docstring = docstring.substring(0, docstring.length() - 1);
		try {
			Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
			ArrayList<String> objs = new ArrayList<String>();
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT distinct o FROM quadruple where doc_id in ("
							+ String.format("%s", docstring)
							+ ") and o like 'b%'");
			while (rs.next()) {
				objs.add(rs.getString(1));
			}
			rs.close();

			if (objs.size() > 0) {
				String ostring = "";
				for (String obj : objs) {
					ostring += "\"" + obj + "\",";
				}
				ostring = ostring.substring(0, ostring.length() - 1);

				ResultSet rs2 = stmt
						.executeQuery("SELECT distinct s from quadruple "
								+ "where s not in (" + ostring + ") "
								+ "and s like 'b%' and doc_id in ("
								+ String.format("%s", docstring) + ")");// 只在主语部分出现的blank
																		// node
				while (rs2.next()) {
					bnids.add(rs2.getString(1));
				}
				rs2.close();
			} else {
				ResultSet rs2 = stmt
						.executeQuery("SELECT distinct s from quadruple "
								+ "where s like 'b%' and doc_id in ("
								+ String.format("%s", docstring) + ")");// 只在主语部分出现的blank
																		// node
				while (rs2.next()) {
					bnids.add(rs2.getString(1).replace(":", "_"));
				}
				rs2.close();
			}
			stmt.close();

			
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bnids;
	}
	/**
	 * 
	 * @param docs
	 *            文檔
	 * @param classes
	 *            被實例化的classes以及他們的子類
	 * @return
	 */
	public static ArrayList<ISentence> find(ArrayList<Integer> docs) {//private 
		ArrayList<ISentence> res = new ArrayList<ISentence>();

		String docstring = "";
		for (int doc : docs) {
			docstring += doc + ",";
		}
		docstring = docstring.substring(0, docstring.length() - 1);
		try {
			HashSet<String> subjects = new HashSet<String>();
			Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
			ArrayList<String> objs = new ArrayList<String>();
			Statement stmt = con.createStatement();
//			ResultSet rs = stmt
//					.executeQuery("SELECT distinct o FROM quadruple where c in ("
//							+ String.format("%s", docstring)
//							+ ") and o like 'b%'");
			ResultSet rs = stmt
					.executeQuery("SELECT distinct o FROM quadruple where doc_id in ("
							+ String.format("%s", docstring)
							+ ") and o like 'b%'");
			while (rs.next()) {
				objs.add(rs.getString(1));
			}
			rs.close();

			if (objs.size() > 0) {
				String ostring = "";
				for (String obj : objs) {
					ostring += "\"" + obj + "\",";
				}
				ostring = ostring.substring(0, ostring.length() - 1);

//				ResultSet rs2 = stmt
//						.executeQuery("SELECT distinct s from quadruple "
//								+ "where s not in (" + ostring + ") "
//								+ "and s like 'b%' and c in ("
//								+ String.format("%s", docstring) + ")");// 只在主语部分出现的blank
//																		// node
				ResultSet rs2 = stmt
						.executeQuery("SELECT distinct s from quadruple "
								+ "where s not in (" + ostring + ") "
								+ "and s like 'b%' and doc_id in ("
								+ String.format("%s", docstring) + ")");// 只在主语部分出现的blank
																		// node
				while (rs2.next()) {
					subjects.add(rs2.getString(1));
				}
				rs2.close();
			} else {
//				ResultSet rs2 = stmt
//						.executeQuery("SELECT distinct s from quadruple "
//								+ "where s like 'b%' and c in ("
//								+ String.format("%s", docstring) + ")");// 只在主语部分出现的blank
//																		// node
				ResultSet rs2 = stmt
						.executeQuery("SELECT distinct s from quadruple "
								+ "where s like 'b%' and doc_id in ("
								+ String.format("%s", docstring) + ")");// 只在主语部分出现的blank
																		// node
				while (rs2.next()) {
					subjects.add(rs2.getString(1));
				}
				rs2.close();
			}
			stmt.close();

			HashMap<String, ISentence> sents = new HashMap<String, ISentence>();
			if (subjects.size() > 0) {
				String s_string = "";
				for (String subject : subjects) {
					s_string += "\"" + subject + "\",";
				}
				s_string = s_string.substring(0, s_string.length() - 1);
//				String selsentstring = String
//						.format("SELECT distinct s, sentence_hash, c FROM quadruple where c in (%s) and s in (%s)",
//								docstring, s_string);
				String selsentstring = String
						.format("SELECT distinct s, sentence_hash, doc_id FROM quadruple where doc_id in (%s) and s in (%s)",
								docstring, s_string);
				Statement selsentstmt = con.createStatement();
				ResultSet selsentrs = selsentstmt.executeQuery(selsentstring);
				while (selsentrs.next()) {
					String s = selsentrs.getString(1);
					byte[] sent = selsentrs.getBytes(2);
					int c = selsentrs.getInt(3);
					if (subjects.contains(s)) {
						if (sents.containsKey(s)) {
							ISentence is = sents.get(s);
							is.addSent(c, sent);
						} else {
							ISentence is = new ISentence(s);
							is.addSent(c, sent);
							sents.put(s, is);
						}
					}
				}
				selsentrs.close();
				selsentstmt.close();
			}
			con.close();

			for (ISentence is : sents.values()) {
				res.add(is);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
}
